package com.njtoyo.taxi.admin.task;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.njtoyo.taxi.admin.mapper.master.entity.RealtimeCall;
import com.njtoyo.taxi.admin.mapper.master.entity.Ride;
import com.njtoyo.taxi.admin.mapper.master.enums.RideStateState;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverConfirmCallPersistedMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverGrabRealtimePersistedMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.RealtimeCallMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.RideMapper;
import com.njtoyo.taxi.admin.mapper.secondary.entity.data.DashboardCallDailyInfo;
import com.njtoyo.taxi.admin.mapper.secondary.mapper.data.DashboardCallDailyInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class DashboardCallDailyInfoTask {

    @Autowired
    private RealtimeCallMapper realtimeCallMapper;
    @Autowired
    private RideMapper rideMapper;
    @Autowired
    private DriverConfirmCallPersistedMapper driverConfirmCallPersistedMapper;
    @Autowired
    private DriverGrabRealtimePersistedMapper driverGrabRealtimePersistedMapper;
    @Autowired
    private DashboardCallDailyInfoMapper dashboardCallDailyInfoMapper;


    /**
     * 每日凌晨2点执行
     */
    @Scheduled(cron = "0 0 2 * * ? ")
    public void dashboardCallDailyInfo() {
        log.info("执行统计漏斗界面数据...");
        Date startDate = DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.date(), -1));
        Date endDate = DateUtil.endOfDay(DateUtil.offsetDay(DateUtil.date(), -1));
        System.out.println("开始时间：" + startDate + "，结束时间：" + endDate);
        List<Map<String, Object>> realtimeCallListMap = realtimeCallMapper.selectMaps(Wrappers.<RealtimeCall>lambdaQuery()
                .between(RealtimeCall::getCreatedAt, startDate, endDate)
                .orderByDesc(RealtimeCall::getId));
        int call_num = realtimeCallListMap.size();

        List<Long> callIds = realtimeCallListMap.stream().map(real -> MapUtil.getLong(real, "id")).collect(Collectors.toList());
        List<Ride> rides = rideMapper.selectList(Wrappers.<Ride>lambdaQuery()
                .in(Ride::getRefId, callIds)
                .in(Ride::getState, Arrays.asList(RideStateState.ended, RideStateState.completed, RideStateState.marked)));
        int completeNum = rides.size();
        Double call_ride = 0.0;

        if (CollUtil.isNotEmpty(realtimeCallListMap)) {
            call_ride = NumberUtil.div(completeNum, call_num, 4) * 100;
        }


        int sendNum = driverConfirmCallPersistedMapper.selectCountSendNum(callIds,0);
        int joinCount = driverGrabRealtimePersistedMapper.selectCountJoinDriver(callIds);
        Double ride = 0.0;

        if (CollUtil.isNotEmpty(realtimeCallListMap)) {
            ride = NumberUtil.div(joinCount, sendNum, 4) * 100;
        }

        System.out.println("订单呼叫数：" + call_num);
        System.out.println("订单完成数：" + completeNum);
        System.out.println("订单完成率：" + call_ride);
        System.out.println("发送司机数：" + sendNum);
        System.out.println("参与抢单的司机数：" + joinCount);
        System.out.println("抢单率：" + ride);
        DashboardCallDailyInfo dashboardCallDailyInfo = new DashboardCallDailyInfo();
        dashboardCallDailyInfo.setCallNum(call_num);
        dashboardCallDailyInfo.setCompleteNum(completeNum);
        dashboardCallDailyInfo.setCompleteRate(Convert.toBigDecimal(call_ride));
        dashboardCallDailyInfo.setSendDriverNum(sendNum);
        dashboardCallDailyInfo.setGrabCallNum(joinCount);
        dashboardCallDailyInfo.setGrabRate(Convert.toBigDecimal(ride));
        dashboardCallDailyInfo.setCreateDate(DateUtil.formatDate(startDate));
        dashboardCallDailyInfoMapper.insert(dashboardCallDailyInfo);
    }
}
